package domain.services;


import domain.entities.ChatMessage;
import infrastructure.Repository;
import internal.InternalResult;
import org.apache.log4j.Logger;

import java.util.Collection;

/**
 * 聊天室消息领域服务
 *
 * @author wyl
 */
public final class ChatMessageDomainService {
    public static ChatMessageDomainService Instance = new ChatMessageDomainService();
    private final Logger logger = Logger.getLogger(this.getClass());
    private Repository<ChatMessage> repository;

    public ChatMessageDomainService() {
        try {
            this.repository = new Repository<ChatMessage>() {
            };
        } catch (Exception e) {
            logger.fatal("ChatMessageDomainService创建失败");
            e.printStackTrace();
        }
    }


    public InternalResult add(ChatMessage message) {
        if (repository.add(message)) {
            return InternalResult.createSuccess();
        }
        return InternalResult.crateError("存入数据库失败", false);
    }

    public Collection<ChatMessage> getAllMessage() {
        return repository.queryAll();
    }

    public InternalResult removeMessageById(Long id) {
        if (repository.deleteById(id)) {
            return InternalResult.createSuccess();
        }
        return InternalResult.crateError("删除消息失败", false);
    }

}
